class Solution:
    def trap(self, height):
        n = len(height)

        left = 0
        right = len(height)-1
        left_max = 0
        right_max = 0
        ans = 0

        while left <= right:
            left_max = max(left_max, height[left])
            right_max = max(right_max, height[right])

            if left_max < right_max:
                ans += left_max - height[left]

            else:
                ans+= right_max -height[right]
        return ans
    


